package com.ruoyi.common.exception.base

import com.ruoyi.common.utils.MessageUtils
import com.ruoyi.common.utils.MyStringUtils

/**
 * 基础异常
 *
 * @author ruoyi
 */
open class BaseException(
        /**
         * 所属模块
         */
        val module: String?,
        /**
         * 错误码
         */
        val code: String?, args: Array<Any>?, defaultMessage: String?) : RuntimeException() {

    /**
     * 错误码对应的参数
     */
    private val args: Array<Any>?

    /**
     * 错误消息
     */
    private val defaultMessage: String?

    init {
        this.args = args
        this.defaultMessage = defaultMessage
    }

    constructor(module: String?, code: String?, args: Array<Any>?) : this(module, code, args, null)
    constructor(module: String?, defaultMessage: String?) : this(module, null, null, defaultMessage)
    constructor(code: String?, args: Array<Any>?) : this(null, code, args, null)
    constructor(defaultMessage: String?) : this(null, null, null, defaultMessage)

    override val message: String?
        get() {
            var message: String? = null
            if (!MyStringUtils.isEmpty(code)) {
                message = MessageUtils.message(code, args)
            }
            return message?:defaultMessage
        }

    fun getArgs(): Array<Any>? {
        return args
    }

    companion object {
        private const val serialVersionUID = 1L
    }
}
